home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16191 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.0 KB  |  179 lines

  1. Path: prodigy.com!usenet
  2. From: XKWR65B@prodigy.com (Mark Rubelmann)
  3. Newsgroups: comp.lang.c++
  4. Subject: Need help with PCX loader!
  5. Date: 9 Apr 1996 21:11:59 GMT
  6. Organization: Prodigy Services Company  1-800-PRODIGY
  7. Distribution: world
  8. Message-ID: <4kejqv$h9s@usenetw1.news.prodigy.com>
  9. NNTP-Posting-Host: innugap7-int.news.prodigy.com
  10. X-Newsreader: Version 1.2
  11.  
  12. I was wondering if anyone had a clue as to what's wrong with this code. 
  13. I've
  14. been messing around with this for several days now and I'm stumped. When 
  15. this is executed, all it does is put a bunch of crap on the screen. I've 
  16. left a few functions out of the code below. (Stuff like changing the 
  17. video mode and
  18. palette routines) This program uses mode 13h, just so you know.. The 
  19. problem is in one of these functions. Perhaps the _fmemcpy?? Anyway, 
  20. here's the code:     
  21. (BTW: This is my first try at classes..)
  22.  
  23.  
  24.  
  25. #include <mem.h>
  26. #include <iostream.h>
  27. #include <fstream.h>
  28. #include <iomanip.h>
  29.  
  30.  
  31. unsigned char far *video_buffer = (char far *)0xA0000000L; // vram ptr
  32.  
  33.  
  34. typedef struct RGB_color_typ
  35. {
  36.     unsigned char red;      //Red component of color (0-63)
  37.     unsigned char green;    //Green component of color (0-63)
  38.     unsigned char blue;     //Blue component of color (0-63)
  39. } RGB_color, *RGB_color_ptr;
  40.  
  41. typedef struct PCX_header_typ
  42. {
  43.     char manufacturer;      // Always 10
  44.     char version;           // 0-v2.5, 2-v2.8, 3-v2.8(Use def. pal), 5-v3.
  45. 0
  46.     char encoding;          // Always 1 (RLE)
  47.     char bits_per_pixel;    // Bits per pixel; in this case, 8.
  48.     int xmin,ymin;          // Upper left corner of image
  49.     int xmax,ymax;          // Size of image
  50.     int hres;               // # of pixels in X direction
  51.     int vres;               // # of pixels in Y direction
  52.     char ega_pal[48];       // EGA palette.. Ignore it..
  53.     char reserved;          // Reserved
  54.     char color_planes;      // # of planes
  55.     int bytes_per_line;     // # of bytes per horiz line
  56.     int palette_type;       // Type of palette
  57.     char filler[58];        // Filler
  58. } PCX_header;
  59.  
  60. typedef struct PCX_struct_typ
  61. {
  62.     PCX_header header;      // 128 byte header
  63.     unsigned char *image;   // Decompressed image
  64.     RGB_color palette[256]; // The palette
  65. } PCX_struct;
  66.  
  67.  
  68. class PCX_Class
  69. {
  70. public:
  71.     int Load(char *filename, PCX_struct *pcx);
  72.     void Show(PCX_struct *pcx);
  73. protected:
  74.     ifstream infile;
  75.     int InitPCX(char *filename, PCX_struct *pcx);
  76.     void LoadPCX(PCX_struct *pcx);
  77. };
  78.  
  79.  
  80. // PCX_Class::Load - Main function to load PCXes
  81. int PCX_Class::Load(char *filename, PCX_struct *pcx)
  82. {
  83.     if(InitPCX(filename, pcx)==1)
  84.         return 1;
  85.     else if(InitPCX(filename, pcx)==2)
  86.         return 2;
  87.     LoadPCX(pcx);
  88.     return 0;
  89. } // End load
  90.  
  91. int PCX_Class::InitPCX(char *filename, PCX_struct *pcx)
  92. {
  93.     ifstream infile(filename, ios::in | ios::nocreate | ios::binary);
  94.     if(infile.bad())
  95.         return 1;
  96.  
  97.     if(!(pcx->image = new unsigned char [64000]))
  98.         return 2;
  99.  
  100.     return 0;
  101. } // End InitPCX
  102.  
  103. void PCX_Class::LoadPCX(PCX_struct *pcx)
  104. {
  105.     unsigned char data, clrdat;
  106.     int numbytes;
  107.     unsigned int count=0;
  108.  
  109.     infile.istream::seekg(0L, ios::beg); // Goto begining of file
  110.     infile >> pcx->header.manufacturer  // Load the header
  111.            >> pcx->header.version
  112.            >> pcx->header.encoding
  113.            >> pcx->header.bits_per_pixel
  114.            >> pcx->header.xmin >> pcx->header.ymin
  115.            >> pcx->header.xmax >> pcx->header.ymax
  116.            >> pcx->header.hres >> pcx->header.vres
  117.            >> pcx->header.ega_pal[48]
  118.            >> pcx->header.reserved
  119.            >> pcx->header.color_planes
  120.            >> pcx->header.bytes_per_line
  121.            >> pcx->header.palette_type
  122.            >> pcx->header.filler[58];
  123.  
  124.     infile.istream::seekg(128L, ios::beg);  // Start of RLE image data
  125.     while(count<=64000)
  126.     {
  127.         infile >> data; // Get first piece o' data
  128.         if(192<=data<=255)   // Is it RLE?
  129.         {
  130.             numbytes=data-192; // # of bytes in run
  131.             infile >> data;     // Data for run
  132.             while(numbytes>0)
  133.             {
  134.                 pcx->image[count++] = data; // Copy the data numbytes 
  135. times
  136.                 numbytes--;
  137.             } // End while
  138.         } // End if RLE
  139.         else
  140.             pcx->image[count++] = data; // If it's not RLE just copy the 
  141. data
  142.     } // End while count... blah blah blah...
  143.  
  144.     infile.istream::seekg(-768L,ios::end);  // Go to begining of palette
  145.     for(int color=0; color<256; color++) // Load palette
  146.     {
  147.         infile >> clrdat;
  148.         clrdat >> 2;
  149.         pcx->palette[color].red = clrdat;
  150.  
  151.         infile >> clrdat;
  152.         clrdat >> 2;
  153.         pcx->palette[color].green = clrdat;
  154.  
  155.         infile >> clrdat;
  156.         clrdat >> 2;
  157.         pcx->palette[color].blue = clrdat;
  158.     } // End load pal
  159. } // End LoadPCX
  160.  
  161. void PCX_Class::Show(PCX_struct *pcx)
  162. {
  163.     _fmemcpy((char far *)video_buffer, pcx->image, 64000);
  164. }
  165.  
  166. void main()
  167. {
  168.     PCX_struct test;
  169.     PCX_Class pcx;
  170.  
  171.     pcx.Load("test.pcx", &test);    // TEST.PCX must be 320x200
  172.     pcx.Show(&test);
  173. }
  174.  
  175.  
  176.  
  177. Thanks! -Mark
  178.  
  179.